﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
namespace ERP.Classes
{
    public class cs_erp_chitiet_hoadonInfo
    {
        private int _id_ct_hoadon;
        private int _id_hoadon;
        private int _id_monan;
        private double _soluong;
        private double _dongia;
        private double _thanhtien;

        public cs_erp_chitiet_hoadonInfo(int id_ct_hoadon, int id_hoadon, int id_monan, double soluong, double dongia, double thanhtien)
        {
            this._id_ct_hoadon = id_ct_hoadon;
            this._id_hoadon = id_hoadon;
            this._id_monan = id_monan;
            this._soluong = soluong;
            this._dongia = dongia;
            this._thanhtien = thanhtien;

        }
        public cs_erp_chitiet_hoadonInfo()
        {
            this._id_ct_hoadon = 0;
            this._id_hoadon = 0;
            this._id_monan = 0;
            this._soluong = 0;
            this._dongia = 0;
            this._thanhtien = 0;

        }
        public int id_ct_hoadon
        {
            get
            {
                return this._id_ct_hoadon;
            }
            set
            {
                this._id_ct_hoadon = value;
            }
        }
        public int id_hoadon
        {
            get
            {
                return this._id_hoadon;
            }
            set
            {
                this._id_hoadon = value;
            }
        }
        public int id_monan
        {
            get
            {
                return this._id_monan;
            }
            set
            {
                this._id_monan = value;
            }
        }
        public double soluong
        {
            get
            {
                return this._soluong;
            }
            set
            {
                this._soluong = value;
            }
        }
        public double dongia
        {
            get
            {
                return this._dongia;
            }
            set
            {
                this._dongia = value;
            }
        }
        public double thanhtien
        {
            get
            {
                return this._thanhtien;
            }
            set
            {
                this._thanhtien = value;
            }
        }

        public int ExcuteStoreProcedure(string storeProcedure, int option)
        {
            string[] paraName = new string[7];
            paraName[0] = "@id_ct_hoadon";
            paraName[1] = "@id_hoadon";
            paraName[2] = "@id_monan";
            paraName[3] = "@soluong";
            paraName[4] = "@dongia";
            paraName[5] = "@thanhtien";
            paraName[6] = "@option";
            object[] paraValue = new object[7];
            paraValue[0] = this._id_ct_hoadon;
            paraValue[1] = this._id_hoadon;
            paraValue[2] = this._id_monan;
            paraValue[3] = this._soluong;
            paraValue[4] = this._dongia;
            paraValue[5] = this._thanhtien;
            paraValue[6] = option;
            csCSDL csdl = new csCSDL();
            int i = 0;
            i = csdl.CreateParameters(paraName, 7, paraValue);
            return csdl.ExcuteStoreProcedure(storeProcedure);
        }
        
        public int ExcuteProcedure(int option)
        {
            return this.ExcuteStoreProcedure("Proc_ERP_CHITIET_HOADON", option);
        }

        
        public int Get_Info()
        {
            csCSDL csdl = new csCSDL();
            DataSet ds = new DataSet("ds");
            try
            {
                ds = csdl.OpenTable("ERP_CHITIET_HOADON", "id_ct_hoadon = " + this._id_ct_hoadon);
                if (ds == null || ds.Tables[0].Rows.Count == 0)
                {
                    return 0;
                }
                this._id_ct_hoadon = Convert.ToInt32(ds.Tables[0].Rows[0]["id_ct_hoadon"].ToString().Trim());
                this._id_hoadon = Convert.ToInt32(ds.Tables[0].Rows[0]["id_hoadon"].ToString().Trim());
                this._id_monan = Convert.ToInt32(ds.Tables[0].Rows[0]["id_monan"].ToString().Trim());
                this._soluong = Convert.ToDouble(ds.Tables[0].Rows[0]["soluong"].ToString().Trim());
                this._dongia = Convert.ToDouble(ds.Tables[0].Rows[0]["dongia"].ToString().Trim());
                this._thanhtien = Convert.ToDouble(ds.Tables[0].Rows[0]["thanhtien"].ToString().Trim());
                return 1;
            }
            catch
            {
                return -1;
            }
        }

        public DataSet Get_List()
        {
            csCSDL csdl = new csCSDL();
            DataSet ds = new DataSet("ds");
            try
            {
                string sql = "select row_number() over (order by b.tenhh) AS stt, a.id_ct_hoadon, a.id_hoadon, a.id_monan, b.tenhh, a.soluong, a.dongia, a.thanhtien, c.donvitinh as dvt"
                + " from ERP_CHITIET_HOADON a, ERP_HANGHOA b, ERP_DVT c"
                + " where b.dvt = c.id_dvt and a.id_monan = b.id_hh and a.id_hoadon = " + this._id_hoadon
                + " and a.soluong > 0";
                ds = csdl.SelecSQL(sql);

                return ds;
            }
            catch
            {
                return null;
            }
        }

    }
}
